package cn.com.cifi.mars.service.impl;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

import cn.com.cifi.mars.bean.bo.SysLogBO;
import cn.com.cifi.mars.bean.dto.DictionaryTypeDto;
import cn.com.cifi.mars.common.exception.MsgException;
import cn.com.cifi.mars.constant.CodeEnum;
import cn.com.cifi.mars.entity.common.SysLog;
import cn.com.cifi.mars.mapper.mars.SysLogMapper;
import cn.com.cifi.mars.service.SysLogService;
import cn.com.cifi.mars.util.UUIDUtils;

@Service
public class SysLogServiceImpl implements SysLogService {

	@Autowired
	private SysLogMapper sysLogMapper;
	/**
	 * 添加日志信息
	 */
	@Override
	public boolean add(SysLogBO bo, String userName) {
		SysLog log = new SysLog();
		// 输入 输出
		BeanUtils.copyProperties(bo, log);
		log.setSource(bo.getSource().getValue());
		log.setEvent(bo.getEvent().getValue());
		//用户不能为空
		log.setUseraccount(userName);
		if("".equals(userName)) {
			throw new MsgException(CodeEnum.USER_ERROR);
		}
		log.setCreateTime(new Date());
		log.setId(UUIDUtils.create());
		try {
			sysLogMapper.insertSelective(log);
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}
		return true;
	}

	/**
	 * 分页查询日志信息
	 */
	@Override
	public Map<String, Object> queryLogPage(String current, String pageSize, String source, String event,
			String title,String content,String type) {
		Page<DictionaryTypeDto> pageDto = new Page<DictionaryTypeDto>();
		// 参数当前页
		pageDto.setCurrent(Integer.parseInt(current));
		// 参数每页数量
		pageDto.setSize(Integer.parseInt(pageSize));
		List<Map<String, Object>> list = sysLogMapper.selectLogPage(pageDto, source, event, title,content,type);
		Map<String, Object> map = new HashMap<>();
		// 当前第几页
		map.put("current", pageDto.getCurrent());
		// 每页多少条
		map.put("pageSize", pageDto.getSize());
		// 总条数
		map.put("total", pageDto.getTotal());
		// 总页数
		map.put("pages", pageDto.getPages());
		map.put("list", list);
		return map;
	}
	
	/**
	 * 根据日期删除日志
	 */
	@Override
	public int deleteLog(int day) {
		return sysLogMapper.deleteLog(day); 
	}

	@Override
	public void updateById(SysLog syslog) {
		sysLogMapper.updateSysLogById(syslog);
	}

	@Override
	public void handleData(String strInfo) {
		sysLogMapper.handleData(strInfo);
	}

}
